package edu.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

/**
 * 单文件上传
 * Part getPart( String name ) // 适用于单个文件上传
 * Collection getParts()// 适用于多个文件上传
 */
public class FileUpload extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        Part fileName = request.getPart("fileName");//name要与input的name相同，否则返回null；
        System.out.println(fileName);   //Part{n=fileName,fn=2020学年缴费票据.png,ct=image/png,s=3096979,t=true,f=null}
        System.out.println(fileName.getName()); //fileName
        System.out.println(fileName.getContentType());  // image/png
        System.out.println(fileName.getSize()); //3096979
        System.out.println(fileName.getSubmittedFileName());    //2020学年缴费票据.png

        fileName.write(fileName.getSubmittedFileName());
    }
}
